goog.provide('dkpoints.nav.LabsHandler');

goog.require('dkpoints.auth.AuthState');
goog.require('dkpoints.labs.templates');
goog.require('dkpoints.ui.RaidMemberList');

goog.require('goog.dom');



/**
 * Handler for the Labs protion, for ease of testing new implementations.
 * @param {dkpoints.ServiceProvider} serviceProvider The provider instance for
 *    the app's subsystems.
 * @constructor
 * @implements {dkpoints.nav.Handler}
 */
dkpoints.nav.LabsHandler = function(serviceProvider) {
  /**
   * @type {Element}
   * @private
   */
  this.bodyContent_ = serviceProvider.getBodyContent();

  /**
   * @type {dkpoints.status.Status}
   * @private
   */
  this.status_ = serviceProvider.getStatusManager();
};


/**
 * No access restrictions to the Labs page.
 * @type {dkpoints.auth.AuthState}
 * @const
 */
dkpoints.nav.LabsHandler.requiredAuth = dkpoints.auth.AuthState.NO_AUTH;


/**
 * The call to action for a navigation handler.
 * @param {dkpoints.nav.Path} path An object with request-specific parameters.
 */
dkpoints.nav.LabsHandler.prototype.handle = function(path) {
  var dom = goog.dom.getDomHelper(this.bodyContent_),
      players = [
        new dkpoints.models.Raider('Amira',
                                   dkpoints.models.Raider.Class.WARLOCK),
        new dkpoints.models.Raider('Antec',
                                   dkpoints.models.Raider.Class.WARLOCK),
        new dkpoints.models.Raider('Avatré',
                                   dkpoints.models.Raider.Class.PRIEST),
        new dkpoints.models.Raider('Bakayarou',
                                   dkpoints.models.Raider.Class.ROGUE),
        new dkpoints.models.Raider('Cobi',
                                   dkpoints.models.Raider.Class.DEATHKNIGHT),
        new dkpoints.models.Raider('Crackseed',
                                   dkpoints.models.Raider.Class.HUNTER),
        new dkpoints.models.Raider('Darktee',
                                   dkpoints.models.Raider.Class.DEATHKNIGHT),
        new dkpoints.models.Raider('Drakengard',
                                   dkpoints.models.Raider.Class.HUNTER),
        new dkpoints.models.Raider('Kiwifruit',
                                   dkpoints.models.Raider.Class.PRIEST),
        new dkpoints.models.Raider('Kuroneko',
                                   dkpoints.models.Raider.Class.WARRIOR),
        new dkpoints.models.Raider('Leilei',
                                   dkpoints.models.Raider.Class.PRIEST),
        new dkpoints.models.Raider('Leoban',
                                   dkpoints.models.Raider.Class.MAGE),
        new dkpoints.models.Raider('Machinshin',
                                   dkpoints.models.Raider.Class.SHAMAN),
        new dkpoints.models.Raider('Moohamed',
                                   dkpoints.models.Raider.Class.DRUID),
        new dkpoints.models.Raider('Mâwt',
                                   dkpoints.models.Raider.Class.DRUID),
        new dkpoints.models.Raider('Narcis',
                                   dkpoints.models.Raider.Class.MAGE),
        new dkpoints.models.Raider('Nïobe',
                                   dkpoints.models.Raider.Class.WARRIOR),
        new dkpoints.models.Raider('Phaetoc',
                                   dkpoints.models.Raider.Class.PALADIN),
        new dkpoints.models.Raider('Phuska',
                                   dkpoints.models.Raider.Class.ROGUE),
        new dkpoints.models.Raider('Piorun',
                                   dkpoints.models.Raider.Class.SHAMAN),
        new dkpoints.models.Raider('Stinkweasel',
                                   dkpoints.models.Raider.Class.ROGUE),
        new dkpoints.models.Raider('Tectuktitlay',
                                   dkpoints.models.Raider.Class.PRIEST),
        new dkpoints.models.Raider('Tog',
                                   dkpoints.models.Raider.Class.DRUID),
        new dkpoints.models.Raider('Xanidus',
                                   dkpoints.models.Raider.Class.PALADIN),
        new dkpoints.models.Raider('Zalos',
                                   dkpoints.models.Raider.Class.DEATHKNIGHT)
      ];

  var editableMemberList;
  /**
   * @type {dkpoints.ui.RaidMemberList}
   * @private
   */
  this.editableMemberList_ = editableMemberList =
      new dkpoints.ui.RaidMemberList(players, true, dom);
  editableMemberList.render(this.bodyContent_);

  var handler;
  /**
   * @type {goog.events.EventHandler}
   * @private
   */
  this.handler_ = handler = new goog.events.EventHandler(this);

  handler.listen(editableMemberList, [
    dkpoints.ui.RaidMemberList.EventType.ADD,
    dkpoints.ui.RaidMemberList.EventType.REMOVE
  ], this.handleEvent);
};


/**
 * When the handler instance is already active, transition will be called on
 * new navigations instead of handle to allow the handler to more easily
 * transition set up the new state of the active driver instead of rebuilding
 * it from scratch.
 * @param {dkpoints.nav.Path} path An object with request-specific parameters.
 * @param {function()} onCancel Callback to be invoked if user decides
 *    not to navigate away from what they're doing.
 */
dkpoints.nav.LabsHandler.prototype.transition = function(path, onCancel) {
  this.handle(path);
};


/**
 * Catch a Raid Member List event.
 * @param {dkpoints.ui.RaidMemberList.Event} e The event.
 */
dkpoints.nav.LabsHandler.prototype.handleEvent = function(e) {
  alert('Event type: ' + e.type + '; Player name: ' + e.name);
};


/**
 * Save state and clear the user-interface so the next handler can use the
 * screen.
 * @param {function(boolean)} onExit Callback once exit is either
 *    complete or cancelled by the user.  Passing true to the callback means
 *    that the handler has relinquished control of the page; false means
 *    the user opted not to navigate away.
 */
dkpoints.nav.LabsHandler.prototype.exit = function(onExit) {
  this.handler_.dispose();
  this.handler_ = null;

  this.editableMemberList_.dispose();
  this.editableMemberList_ = null;

  onExit(true);
};
